<template>
  <div class="role-container">
    <div class="app-container">
      <el-row style="height: 60px;">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="dialogVisible = true">
          新增角色
        </el-button>
      </el-row>
      <el-table
        :data="roleList"
        border
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="240">
          <template #default="{ row }">
            <el-button type="success" size="small" @click="showAssignPermision(row.id)">分配权限</el-button>
            <el-button type="primary" size="small" @click="showDialog(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px;">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="params.page"
          :page-size="params.pagesize"
          layout="prev, pager, next"
          :total="total">
        </el-pagination>
      </el-row>
    </div>
    <!-- 新增/编辑角色弹窗 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '新增角色'"
      :visible="dialogVisible"
      width="50%"
      @close="dialogClose">
      <!-- 表单内容 -->
      <el-form ref="roleRef" :model="roleForm" :rules="roleRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限弹窗 -->
    <assign-permission 
      :visible="assignPermVisible"
      :roleId="roleId"
      @close-assign-permission="closeAssignPermission">
    </assign-permission>
  </div>
</template>

<script>
import { 
  getRoleList,
  addRole,
  getRoleDetail,
  updateRole,
  deleteRole 
} from '@/api/setting'
import AssignPermission from './components/assign-permission.vue'

export default {
  components: {
    AssignPermission
  },
  data() {
    return {
      // 表格数据
      roleList: [],
      // 分页参数
      params: {
        page: 1, // 第几页
        pagesize: 4 // 每页多少条
      },
      // 总条数
      total: 0,
      // 控制弹窗显示与隐藏
      dialogVisible: false,
      // 表单数据对象
      roleForm: {
        name: '',
        description: ''
      },
      // 表单验证规则对象
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      // 控制分配权限弹窗的显示与隐藏
      assignPermVisible: false,
      //角色id
      roleId:''
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    // 获取角色列表数据
    async fetchRoleList() {
      const { data: { rows, total } } = await getRoleList(this.params)
      this.roleList = rows
      this.total = total
    },
    // 当前展示第几页发生变化
    handleCurrentChange(page) {
      // page 当前是第几页
      this.params.page = page
      this.fetchRoleList()
    },
    // 弹窗关闭
    dialogClose() {
      this.dialogVisible = false
      // 清空表单内容
      this.$refs.roleRef.resetFields()
      // 手动清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 新增角色
    submit() {
      // 表单兜底校验
      this.$refs.roleRef.validate(async valid => {
        if (!valid) return
        // 校验通过
        // 发起请求
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        // 提示用户
        const msg = this.roleForm.id ? '编辑角色成功': '新增角色成功'
        this.$message.success(msg)
        // 关闭弹窗
        this.dialogVisible = false
        // 刷新列表
        this.fetchRoleList()
      })
    },
    // 打开弹窗
    async showDialog(id) {
      this.dialogVisible = true
      // 数据回显
      const res = await getRoleDetail(id)
      this.roleForm = res.data
    },
    // 删除角色
    deleteRole(id) {
      // 询问用户
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起请求
        await deleteRole(id)
        // 提示用户
        this.$message.success('删除角色成功')
        // 刷新列表
        this.fetchRoleList()
      })
    },
    // 打开分配权限弹窗
    showAssignPermision(id) {
      this.assignPermVisible = true
      this.roleId = id
    },
    // 关闭分配权限弹窗
    closeAssignPermission() {
      this.assignPermVisible = false
    }
  }
}
</script>